package com.lcz.googleplay.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lcz.googleplay.adapter.SubjectAdapter;
import com.lcz.googleplay.config.SubjectProtocol;
import com.lcz.googleplay.domain.SubjectInfo;
import com.lcz.googleplay.utils.UIUtils;
import com.lcz.googleplay.widget.LoadingFrameLayout;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by hp on 2016/10/25.
 */

public class SubjectFragment extends BaseFragment {

    private List<SubjectInfo> subjectInfo;

    @Override
    public View createSuccessView() {
        ListView listView = new ListView(UIUtils.getActivity());
        listView.setAdapter(new SubjectAdapter(subjectInfo,listView) {
            @Override
            protected List<SubjectInfo> onload() {
                SubjectProtocol subjectProtocol = new SubjectProtocol();
                String json = subjectProtocol.load(subjectInfo.size());
                Type type = new TypeToken<List<SubjectInfo>>() {
                }.getType();
                Gson gson = new Gson();
                List<SubjectInfo> newSubjectInfo = gson.fromJson(json, type);
                subjectInfo.addAll(newSubjectInfo);
                return newSubjectInfo;
            }
        });
        return listView;
    }

    @Override
    public LoadingFrameLayout.LoadResult load() {
        SubjectProtocol subjectProtocol = new SubjectProtocol();
        String json = subjectProtocol.load(0);
        Type type = new TypeToken<List<SubjectInfo>>() {
        }.getType();
        Gson gson = new Gson();
        subjectInfo = gson.fromJson(json, type);
        return checkData(subjectInfo);
    }
}
